<template>
  <div>
    <h1>{{id ? '修改' : '新建'}}铭文</h1>
    <el-form label-width="120px" @submit.native.prevent="save">
      <el-form-item label="名称">
        <el-input v-model="model.ming_name"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-select v-model="model.ming_type">
          <el-option value="red">red</el-option>
          <el-option value="blue">blue</el-option>
          <el-option value="green">green</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="等级">
        <el-select v-model="model.ming_grade">
          <el-option v-for="item in 5" :key="item" :value="item" :label="item"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="model.ming_des"></el-input>
      </el-form-item>
      <el-form-item label="图标">
        <el-input v-model="model.ming_id"></el-input>
        <img v-if="model.ming_id" class="ming_img" :src="`https://game.gtimg.cn/images/yxzj/img201606/mingwen/${model.ming_id}.png`">
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: String
  },
  data () {
    return {
      model: {}
    }
  },
  methods: {
    async save () {
      if (this.model.ming_name.trim() === '') {
        this.$message.error('请输入铭文名称')
        return
      }
      if (this.id) {
        await this.$http.put(`/rest/mings/${this.id}`, this.model)
        this.$message.success('修改成功')
      } else {
        await this.$http.post('/rest/mings', this.model)
        this.$message.success('保存成功')
      }
      this.$router.push('/mings/list')
    },
    async getName () {
      const { data: res } = await this.$http.get(`/rest/mings/${this.id}`)
      this.model = res
    }
  },
  created () {
    this.id && this.getName()
  }
}
</script>

<style scoped>
.el-input {
  width: 220px;
}
.ming_img {
  position: absolute;
  top: -6px;
  margin-left: 5px;
}
</style>
